// pages/index/detail/discuss/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comment_box: [
    ],
    stars: [
      { img: "/pages/icon/favorite1.png" },
      { img: "/pages/icon/favorite1.png" },
      { img: "/pages/icon/favorite1.png" },
      { img: "/pages/icon/favorite1.png" },
      { img: "/pages/icon/favorite1.png" },
    ],
    imgurl: app.urlimg,
    synthesize: 4,//综合评分
    describe: 4,//描述相符
    serve: 3,//服务态度
    logistics: 4,//物流服务
    goods_id: 0,
    label_id:0,
    label_type: 0,
    page:1,
    pagesize:10,
		imgurl2: app.urlimg,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      goods_id: options.goods_id,
      label_id: options.label_id,
      label_type: options.type
    });
    app.getUserInfo(function(token_key){
      wx.request({
        url: app.url + 'api_goods_comment/commentList',
        data: {
          goods_id: that.data.goods_id,
          type: that.data.label_type,
          label_id: that.data.label_id,
          pagesize: that.data.pagesize,
        },
        header: {
          'content-type': 'application/json',
          'token': token_key
        }, success: function (json) {
					//判断是否有空置的图片
					for (var i = 0; i < json.data.result.comment_list.length;i++){
						var list_img=[];
						for (var g = 0; g < json.data.result.comment_list[i].comment_img.length;g++){
							if(json.data.result.comment_list[i].comment_img[g]!=''){
								list_img.push(json.data.result.comment_list[i].comment_img[g])
							}
						}
						json.data.result.comment_list[i].comment_img = list_img;
					}
          if(json.data.done) {
            that.setData({
              comment_box:json.data.result.comment_list,
              synthesize: json.data.result.point.total,
              describe: json.data.result.point.desc_point,
              logistics: json.data.result.point.logistics_point,
              serve: json.data.result.point.serve_point,
            });
          } else {
            //失败
            app.apperror(json)
          }
      
        }, complete: function () {
      
        }
      })
    });
  },
	// 查看图片
	previewImage: function (e) {
		var that = this;
		//获取当前图片的下表
		console.log(e);
		var box = e.currentTarget.dataset.box;//获取评论的下标
		var index = e.currentTarget.dataset.index;//获取评论图片的下标
		console.log(box);
		console.log(index);
		//数据源
		var comment_img = that.data.comment_box[box].comment_img;//获取被点击的评论图片数组
		var bun = [];
		for (var i = 0; i < comment_img.length; i++) {
			var all = that.data.imgurl2 + comment_img[i];
			bun.push(all)
		}
		console.log(bun);
		wx.previewImage({
			//当前显示下表
			current: bun[index],
			//数据源
			urls: bun,
		})
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    wx.request({
      url: app.url + 'api_goods_comment/commentList',
      data: {
        goods_id: that.data.goods_id,
        page: ++page,
        type: that.data.label_type,
        label_id: that.data.label_id,
        pagesize: that.data.pagesize,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        if (res.data.result.comment_list.length > 0) {
          var goods = that.data.comment_box;
          var newForum = goods.concat(res.data.result.comment_list);
          that.setData({ page: page, comment_box: newForum });
          console.log(that.data.comment_box);
        } else {
          that.setData({ toastHidden: false })
        }

      }, complete: function () {
        //显示出加载中的提示
        that.setData({ loadHidden: true })
      }
    })

  },
 
})